
var page = 1,limit=3;
var totalPage = 0;

window.addEventListener('onDropdown', function(event){
    //console.log('下接刷新：' );

    if(page<totalPage){
        page++;

        loadData(this.page,this.limit);
    }else{
        console.log(page)
      //  pop_up("没有更多了...")
    }
});
var meth={

      top_route:1,

    toproute:  function (m){
        meth.initRoute();
        if(m=="m1"){
            loadTeachers(1);
            $("#m1").css("color","#f23e3d");
            $("#top_route").css("position","absolute");
            $("#top_route").css("margin-left","0px");
            $("#top_route").css("top","30px");

        }else if(m=="m2"){
            loadTeachers(2);
            $("#m2").css("color","#f23e3d");
            $("#top_route").css("margin-left","33%");
            $("#top_route").css("position","absolute");
            $("#top_route").css("top","30px");

        }else if(m=="m3"){
            loadTeachers(3);
            $("#m3").css("color","#f23e3d");
            $("#top_route").css("margin-left","66%");
            $("#top_route").css("position","absolute");
            $("#top_route").css("top","30px");

        }
    },
    initRoute: function (){
        $("#m1").css("color","#000000");
        $("#m2").css("color","#000000");
        $("#m3").css("color","#000000");
    },
    scoreImage:function (score) {
          var himage="";
        if(score){
            if(score==0||(score>0&&score<1)){
                himage='<img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
            }else if(score==1||(score>1&&score<2)){
                himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
            }else if(score==2||(score>2&&score<3)){
                himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
            }else if(score==3||(score>3&&score<4)){
                himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
            }else if(score==4||(score>4&&score<5)){
                himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star">';
            }else if(score>=5){
                himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star">';
            }
        }else{
            himage='<img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }
        return himage;
    },



};
meth.toproute("m1");
//loadTeachers(1);

function loadTeachers(orderBy) {
    var serviceId = server.getQueryString("serviceId");
    var service = server.getQueryString("service");
  //  console.log("service id = "+serviceId);
    var strquery;
    if(orderBy)
     strquery={serviceId:serviceId,orderBy:orderBy};
    else
        strquery={serviceId:serviceId,orderBy:1};

    if(service)strquery.servicename=service;


    server.req("wechat/teacher/selectTeacherInfoByServiceId",strquery,function (res) {
       console.log("teachers list:");
       console.log(res);
        var strHtml="",noHtml;


        noHtml = '<div class="nodata">\n' +
            '            <div class="nodata-image">\n' +
            '                <img src="img/icons/search.png" />\n' +
            '            </div>\n' +
            '            <div class="nodata-text">\n' +
            '                <label>没有找到任何数据</label>\n' +
            '            </div>\n' +
            '        </div>';
        if(res.code==0){
            var data = res.data;

            if(data.length>0){
                $("#nodata").css("display","none");
                server.loadData("#list",res);
            }
            data.forEach(function (item,index) {
                //处理在线状态
                if(item==null)return;
                if(item.online){
                    item.stronline="在线";
                }else{
                    item.stronline="离线";
                }

                var sp = null;
                if(item.specialty){
                    //sp=item.specialty.split(";");
                    if(item.specialty.indexOf(";")!=-1){
                        sp=item.specialty.split(";");
                    }else if(item.specialty.indexOf(",")!=-1){
                        sp=item.specialty.split(",");
                    }else{
                        sp=item.specialty;
                    }
                }
                var sH = "";
                if(sp) {
                    if( typeof  sp== "string"){
                        item.sH= '<div class="item-row2-right-item"><label>' + sp + '</label></div>'

                    }else{
                        sp.forEach(function (value, index1) {
                            sH = sH + '<div class="item-row2-right-item"><label>' + value + '</label></div>'
                            item.sH = sH;
                        })
                    }

                }else{
                    sH = sH + '<div class="item-row2-right-item-nodata"><label>(专家未设置学术项目)</label></div>'
                    item.sH = sH;
                }
                //处理技能字段的显示
                var vskills = null;
                var skillsHtml = "";
                if(item.skills){
                    //if(item.skills)vskills=item.skills.split(";");

                    if(item.skills.indexOf(";")!=-1){
                        vskills = item.skills.split(";");
                    }else if(item.skills.indexOf(",")!=-1){
                        vskills = item.skills.split(",");
                    }else{
                        vskills = item.skills;
                    }


                    if(vskills) {
                        if( typeof  vskills== "string"){
                            item.skillsHtml  =  '<div class="skills"><label>' + vskills + '</label></div>'
                        }else{
                        vskills.forEach(function (value, index1) {
                            skillsHtml = skillsHtml + '<div class="skills"><label>' + value + '</label></div>'
                            item.skillsHtml = skillsHtml;
                        })
                        }
                    }
                }



                var headimg = item.headimgurl;
                if(server.isEmpty(item.headimgurl))
                    headimg = "img/icons/default.png";

                //根据得分多少显示小星星
                var himage = meth.scoreImage(item.evaluateScore);
                item.himage = himage;
                // strHtml = strHtml + '<div class="item" onclick="todetail('+item.id+')">\n' +
                //     '            <div class="item-row1">\n' +
                //     '                <div class="item-row1-left">\n' +
                //     '                    <image src="'+headimg+'" />\n' +
                //     '                </div>\n' +
                //     '                <div class="item-row1-right">\n' +
                //     '                    <div class="item-row1-right-row1">\n' +
                //     '                        <label class="item-row1-right-row1-text1">'+(item.nickname||'')+'</label><label class="item-row1-right-row1-text2">'+(item.province||'')+'</label>\n' +
                //     '                    </div>\n' +
                //     '                    <div class="item-row1-right-row2">\n' +
                //     '                        <img src="" /> <label class="item-row1-right-row2-text1">'+(item.evaluateScore||0)+'分 '+(item.evaluateNumber||0)+'人评价</label>\n' +
                //     '                    </div>\n' +
                //     '                    <div class="item-row1-right-row3">\n' +
                //     '                        <label class="item-row1-right-row3-text1">'+(item.skills||'')+'</label>\n' +
                //     '                    </div>\n' +
                //     '                </div>\n' +
                //     '            </div>\n' +
                //     '            <div class="item-row2">\n' +
                //     '                <div class="item-row2-left">\n' +
                //     '                    <div class="item-row2-left-row1">\n' +
                //     '                        <label>¥'+(item.minprice||0)+'起</label>\n' +
                //     '                    </div>\n' +
                //     '                    <div class="item-row2-left-row2">\n' +
                //     '                        <label>'+(item.askNumber||0)+'人咨询</label>\n' +
                //     '                    </div>\n' +
                //     '                </div>\n' +
                //     '                <div class="item-row2-right">\n' +sH+
                //     '                    </div>\n' +
                //     '                </div>\n' +
                //     '            </div>\n' +
                //     '        </div>'

            })
            server.loadData("#list",res);
            // $("#list").html(strHtml==""?noHtml:strHtml);
        }
    })
}
function loadData(page,limit) {
    var strHtml="",noHtml;
        noHtml = '<div class="nodata">\n' +
            '            <div class="nodata-image">\n' +
            '                <img src="img/icons/search.png" />\n' +
            '            </div>\n' +
            '            <div class="nodata-text">\n' +
            '                <label>没有找到任何数据</label>\n' +
            '            </div>\n' +
            '        </div>';

    server.req("wechat/teacher/list",{page:page,limit:limit},function (res) {
        console.log(res);
        totalPage = res.totalPage;

        if(res.code == 0){
            var data = res.data;

            if(page==1)strHtml="";
            data.forEach(function (item,index) {
                var sp = null;
                if(item.specialty)sp=item.specialty.split(";");
                var sH = "";
                if(sp) {
                    sp.forEach(function (value, index1) {
                        sH = sH + '<div class="item-row2-right-item"><label>' + value + '</label></div>'

                    })
                }
                var headimg = item.headimgurl;
                if(server.isEmpty(item.headimgurl))
                    headimg = "img/icons/default.png";
                strHtml = strHtml + '<div class="item" onclick="todetail('+item.id+')">\n' +
                    '            <div class="item-row1">\n' +
                    '                <div class="item-row1-left">\n' +
                    '                    <image src="'+headimg+'" />\n' +
                    '                </div>\n' +
                    '                <div class="item-row1-right">\n' +
                    '                    <div class="item-row1-right-row1">\n' +
                    '                        <label class="item-row1-right-row1-text1">'+(item.nickname||'')+'</label><label class="item-row1-right-row1-text2">'+(item.province||'')+'</label>\n' +
                    '                    </div>\n' +
                    '                    <div class="item-row1-right-row2">\n' +
                    '                        <img src="" /> <label class="item-row1-right-row2-text1">'+(item.evaluateScore||0)+'分 '+(item.evaluateNumber||0)+'人评价</label>\n' +
                    '                    </div>\n' +
                    '                    <div class="item-row1-right-row3">\n' +
                    '                        <label class="item-row1-right-row3-text1">'+(item.skills||'')+'</label>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="item-row2">\n' +
                    '                <div class="item-row2-left">\n' +
                    '                    <div class="item-row2-left-row1">\n' +
                    '                        <label>¥'+(item.minprice||0)+'起</label>\n' +
                    '                    </div>\n' +
                    '                    <div class="item-row2-left-row2">\n' +
                    '                        <label>'+(item.askNumber||0)+'人咨询</label>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '                <div class="item-row2-right">\n' +sH+
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '        </div>'

            })
            $("#list").html(strHtml==""?noHtml:strHtml);
        }
    })
};
function todetail(teacherid) {
    server.loadindex('page/teacher/teacherdetail/teacherdetail','teachid='+teacherid,true);
};
function tochat(memberid){
    server.loadindex('page/chat/chat','teacherTopicID='+memberid);
}